package com.shisfish.core.dto;

public class PageData {
	
	private boolean hasPreviousPage; // 是否有上一页
	private boolean hasNextPage; // 是否有下一页
	private int count; // 总条数
	private int pageIndex; // 当前页数
	private int pageSize; // 当前显示页数
	private int beginIndex; // 起始位置
	private int pageCount; // 总页数
	
	public PageData() {}

	public PageData(boolean hasPreviousPage, boolean hasNextPage, int count, int pageIndex, int pageSize,
			int beginIndex, int pageCount) {
		this.hasPreviousPage = hasPreviousPage;
		this.hasNextPage = hasNextPage;
		this.count = count;
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.beginIndex = beginIndex;
		this.pageCount = pageCount;
	}

	public static PageData createPage(int count, int pageIndex, int pageSize) {
		pageIndex = getPageIndex(pageIndex);
		pageSize = getPageSize(pageSize);
		int beginIndex = getBeginIndex(pageIndex, pageSize);
		int pageCount = getPageCount(count, pageSize);
		if (beginIndex > count) {
			if (pageCount > 0)
				pageIndex = pageCount;
			else
				pageIndex = 1;
			beginIndex = getBeginIndex(pageIndex, pageSize);
		}
		boolean hasPreviousPage = isFirstPage(pageIndex);
		boolean hasNextPage = isLastPage(pageIndex, pageCount);
		
		return new PageData(hasPreviousPage, hasNextPage, count, pageIndex, pageSize, beginIndex, pageCount);
	}

	private static boolean isLastPage(int pageIndex, int pageCount) {
		return pageIndex == pageCount || pageCount == 0 ? false : true;
	}

	private static boolean isFirstPage(int pageIndex) {
		return pageIndex == 1 ? false : true;
	}

	private static int getPageCount(int count, int pageSize) {
		if (count == 0) 
			return 0;
		int pageCount = 1;
		if (count % pageSize == 0)
			pageCount = count / pageSize;
		else 
			pageCount = count / pageSize + 1;
		return pageCount;
	}

	private static int getBeginIndex(int pageIndex, int pageSize) {
		return (pageIndex - 1) * pageSize;
	}

	private static int getPageSize(int pageSize) {
		return pageSize <= 0 ? 10 : pageSize; 
	}

	private static int getPageIndex(int pageIndex) {
		return pageIndex == 0 ? 1 : pageIndex;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	

}
